/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.brain.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.common.R;
import com.thinkgem.jeesite.modules.brain.common.MatchType;
import com.thinkgem.jeesite.modules.brain.common.UserStatusUtil;
import com.thinkgem.jeesite.modules.brain.entity.BUserOrprInfo;
import com.thinkgem.jeesite.modules.brain.service.BUserOrprInfoService;
import com.thinkgem.jeesite.modules.brain.service.BUserRankDataService;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.brain.entity.BQuestionBank;
import com.thinkgem.jeesite.modules.brain.service.BQuestionBankService;

import java.util.Map;
import java.util.concurrent.Future;

/**
 * 题库Controller
 * @author 程展峰
 * @version 2021-09-18
 */
@Controller
@RequestMapping(value = "${adminPath}/brain/bQuestionBank")
public class BQuestionBankController extends BaseController {

	@Autowired
	private BQuestionBankService bQuestionBankService;

	@Autowired
	private BUserOrprInfoService bUserOrprInfoService;

	@Autowired
	private BUserRankDataService bUserRankDataService;

	@ModelAttribute
	public BQuestionBank get(@RequestParam(required=false) String id) {
		BQuestionBank entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = bQuestionBankService.get(id);
		}
		if (entity == null){
			entity = new BQuestionBank();
		}
		return entity;
	}
	
	@RequiresPermissions("brain:bQuestionBank:view")
	@RequestMapping(value = {"list", ""})
	public String list(BQuestionBank bQuestionBank, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<BQuestionBank> page = bQuestionBankService.findPage(new Page<BQuestionBank>(request, response), bQuestionBank); 
		model.addAttribute("page", page);
		return "modules/brain/bQuestionBankList";
	}

	@RequiresPermissions("brain:bQuestionBank:view")
	@RequestMapping(value = "form")
	public String form(BQuestionBank bQuestionBank, Model model) {
		model.addAttribute("bQuestionBank", bQuestionBank);
		return "modules/brain/bQuestionBankForm";
	}

	@RequiresPermissions("brain:bQuestionBank:edit")
	@RequestMapping(value = "save")
	public String save(BQuestionBank bQuestionBank, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, bQuestionBank)){
			return form(bQuestionBank, model);
		}
		bQuestionBankService.save(bQuestionBank);
		addMessage(redirectAttributes, "保存题库成功");
		return "redirect:"+Global.getAdminPath()+"/brain/bQuestionBank/?repage";
	}

	@RequiresPermissions("brain:bQuestionBank:edit")
	@RequestMapping(value = "delete")
	public String delete(BQuestionBank bQuestionBank, RedirectAttributes redirectAttributes) {
		bQuestionBankService.delete(bQuestionBank);
		addMessage(redirectAttributes, "删除题库成功");
		return "redirect:"+Global.getAdminPath()+"/brain/bQuestionBank/?repage";
	}
	//
	//點擊練習賽;
	//星數 實時更新 所以不用進行傳輸當前用戶狀態
	//提供習題
	//type  001 排位赛,002练习赛
	@RequestMapping(value = "offer/{type}")
	public R offerQues(@Param("type") int type){
		BQuestionBank[] target=null;

		//用戶狀態檢驗
		/**
		 *  是否可以參加相應比賽
		 */
		//选题
		if(MatchType.practice.getType()==type&& UserStatusUtil.isAllowpratice())
			target=getPracticeQues();
		else if(MatchType.rank.getType()==type && UserStatusUtil.isAllowRank())
			target=getRankQues();



		return  R.ok(target);
	}
	//獲取排位賽
	private BQuestionBank[] getRankQues(){
		//選題算法
		/**
		 * todo 題目根據可配置進行       題型選擇  提供概率 獲取當前用戶的星星數
		 *
		 */
		return null;
	}
	//獲取練習賽
	private BQuestionBank[] getPracticeQues(){
		//選題算法
		/**
		 * todo 題目根據可配置進行       題型選擇  提供概率 獲取當前用戶的星星數
		 *
		 */
		return null;
	}

	//處理提交
	public R handleRecord(BUserOrprInfo record ){
		//用户记录
		HandleRecord(record);

		Map result=null;
		//用户状态
		//修改用户状态
		if(Integer.parseInt(record.getPraticeType())==MatchType.practice.getType()){

			UserStatusUtil.changeStatusAfterAnswer(MatchType.practice,record);

		}else if(Integer.parseInt(record.getPraticeType())==MatchType.rank.getType()){
			//若是排位赛提供修改用户星级

			UserStatusUtil.changeStatusAfterAnswer(MatchType.rank,record);
			result=UserStatusUtil.changeRankStarAndReturnInfo( record);

			//實時,直接入庫
			updateStar((Integer) result.get("allStars"));

		}



		return R.ok(result);

	}

	//開綫程池入庫
	//使用springtask线程池
	@Async
	public void HandleRecord(BUserOrprInfo record){

		//答题记录入库
		bUserOrprInfoService.save(record);

		//return new AsyncResult<String>("hello world !!!!");
	}

	@Async
	public void updateStar(int count){

		//答题记录入库
		bUserRankDataService.updateCurrStar(count);

		//return new AsyncResult<String>("hello world !!!!");
	}

}